package com.hxkj.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hxkj.dao.AdvertisementDao;
import com.hxkj.domain.po.Advertisement;
import com.hxkj.domain.po.AdvertisementColumn;
import com.hxkj.service.AdvertisementColumnService;
import com.hxkj.service.AdvertisementService;
import com.platform.common.domain.result.ResultDataStruct;
import com.platform.common.domain.result.ResultMessageStruct;
import com.platform.common.domain.to.PageTo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 广告管理
 * @author   create by chenfangzhou 2018/09/14
 */
@Service
@Slf4j
public class AdvertisementServiceImpl implements AdvertisementService {

    @Autowired
    private AdvertisementDao advertisementDao;

    @Autowired
    private AdvertisementColumnService advertisementColumnService;

    /**
     * 获取单条广告
     * @param advertisement
     * @return
     * @throws Exception
     */
    @Override
    public ResultDataStruct<Advertisement> get(Advertisement advertisement) throws Exception {
        ResultDataStruct<Advertisement> result = new ResultDataStruct<>();
        try {
            Advertisement advert = advertisementDao.get(advertisement.getAdvertisementId());
            result.setData(advert);
            result.setCode(200);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
        return result;
    }

    /**
     * 保存广告
     * @param advertisement
     * @return
     * @throws Exception
     */
    @Override
    public ResultMessageStruct save(Advertisement advertisement) throws Exception {
        ResultMessageStruct result = new ResultMessageStruct();
        try {
            advertisement.setCreateTime(System.currentTimeMillis());
            advertisement.setUpdateTime(System.currentTimeMillis());
            advertisementDao.save(advertisement);
            result.setCode(200);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
        return result;
    }

    /**
     * 修改广告
     * @param advertisement
     * @return
     * @throws Exception
     */
    @Override
    public ResultMessageStruct modify(Advertisement advertisement) throws Exception {
        ResultMessageStruct result = new ResultMessageStruct();
        try {
            // 防止更改后重复
            if (StringUtils.isNotBlank(advertisement.getAdvertisementIds())) advertisement.setAdvertisementId(null);
            List<Advertisement> list = advertisementDao.checkRepeat(advertisement);
            if (list.size() > 0) {
                result.setCode(401);
                result.setMessage("修改后广告名重复!");
                return result;
            }
            advertisement.setUpdateTime(System.currentTimeMillis());
            advertisementDao.update(advertisement);
            result.setCode(200);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
        return result;
    }

    /**
     * 软删除
     * @param advertisement
     * @return
     * @throws Exception
     */
    @Override
    public ResultMessageStruct remove(Advertisement advertisement) throws Exception {
        ResultMessageStruct result = new ResultMessageStruct();
        try {
            if (StringUtils.isNotBlank(advertisement.getAdvertisementIds())) advertisement.setAdvertisementId(null);
            advertisementDao.delete(advertisement);
            result.setCode(200);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
        return result;
    }

    /**
     * 获取带分页的列表
     * @param advertisement
     * @param pageTo
     * @return
     * @throws Exception
     */
    @Override
    public PageInfo getListWithPage(Advertisement advertisement, PageTo pageTo) throws Exception {
        try {
            if (advertisement.getColumnId() != null) {
                AdvertisementColumn column = new AdvertisementColumn();
                column.setColumnId(advertisement.getColumnId());
                AdvertisementColumn advertisementColumn = advertisementColumnService.getColumn(column);
                if (this.getColumnIds(advertisementColumn) != null) {
                    advertisement.setColumnId(null);
                    advertisement.setColumnIds(this.getColumnIds(advertisementColumn));
                }
            }
            Page<Advertisement> page = PageHelper.startPage(pageTo.getPageNum(), pageTo.getPageSize());
            List<Advertisement> artList = advertisementDao.get(advertisement);
            PageInfo<Advertisement> pageInfo = new PageInfo<>(page);
            pageInfo.setList(artList);
            return pageInfo;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    /**
     * 不带分页的列表
     * @param advertisement
     * @return
     * @throws Exception
     */
    @Override
    public List<Advertisement> getList(Advertisement advertisement) throws Exception {
        try {
            if (advertisement.getColumnId() != null) {
                AdvertisementColumn column = new AdvertisementColumn();
                column.setColumnId(advertisement.getColumnId());
                AdvertisementColumn advertisementColumn = advertisementColumnService.getColumn(column);
                if (this.getColumnIds(advertisementColumn) != null) {
                    advertisement.setColumnId(null);
                    advertisement.setColumnIds(this.getColumnIds(advertisementColumn));
                }
            }
            List<Advertisement> artList = advertisementDao.get(advertisement);
            return artList;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }




    /**
     * 根据传入的分类id获取该分类的所有子级分类的id字符串
     * @param column
     * @return
     * @throws Exception
     */
    private String getColumnIds(AdvertisementColumn column) throws Exception {
        List<AdvertisementColumn> data = advertisementColumnService.getAllColumnList(column).getData();
        if (data == null || data.size() == 0) return null;
        String ids = "";
        for (AdvertisementColumn col : data) {
            ids += col.getColumnId() + ",";
        }
        return ids;
    }

    /**
     * 修改排序
     * @param map
     * @return
     * @throws Exception
     */
    @Override
    public ResultMessageStruct modifySortNum(Map<String, Object> map) throws Exception {
        ResultMessageStruct result = new ResultMessageStruct();
        try {
            Advertisement advertisement = new Advertisement();
            String[] id = map.get("advertisementIds").toString().split(",");
            advertisement.setSiteId(Long.parseLong(map.get("siteId").toString()));
            for (int i = 0; i < id.length; i++) {
                advertisement.setAdvertisementId(Long.parseLong(id[i]));
                advertisement.setSortNumber((long) i);
                advertisementDao.update(advertisement);
            }
            result.setCode(200);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            result.setCode(401);
            result.setMessage(e.getMessage());
        }
        return result;
    }
}
